package com.zfsbs.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tool.utils.utils.ALog;
import com.tool.utils.utils.LogUtils;
import com.tool.utils.utils.NetUtils;
import com.tool.utils.utils.ToastUtils;
import com.zfsbs.R;
import com.zfsbs.common.CommonFunc;
import com.zfsbs.core.action.LoginAction;
import com.zfsbs.core.myinterface.UiAction;

import org.json.JSONException;
import org.json.JSONObject;


public class GetLoginInfoActivity1 extends BaseActivity implements OnClickListener{


    private static final String TAG = "GetLoginInfoActivity1";

    private Button btnGetInfo;
    private EditText edPassWord;
    private EditText edUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_info_get1);
//        AppManager.getAppManager().addActivity(this);
        initTitle("签到");

        btnGetInfo = (Button) findViewById(R.id.id_info_get);
        edPassWord = (EditText) findViewById(R.id.password);
        edUserName = (EditText) findViewById(R.id.id_username);

        edUserName.setText("");
        edUserName.setSelection(edPassWord.getText().length());

        btnGetInfo.setOnClickListener(this);
    }

    private void getInfo(String username, String password) {
        LoginAction loginAction = new LoginAction(GetLoginInfoActivity1.this, new UiAction() {

            @Override
            public void UiResultAction(Activity context, Class<?> cls, Bundle bundle, int requestCode) {
                LogUtils.e("UiResultAction01");
            }

            @Override
            public void UiAction(Activity context, Class<?> cls, Bundle bundle, boolean flag) {
                LogUtils.e("UiAction01");
            }

            @Override
            public void UiAction(Activity context, Class<?> cls, boolean flag) {
                LogUtils.e("UiAction02");
                finish();

                Intent intent222=new Intent();
                intent222.setComponent(new ComponentName("com.fuyousf.android.fuious","com.fuyousf.android.fuious.MainActivity"));
                //包名     包名+类名（全路径）
                intent222.putExtra("transName", "签到");
                startActivityForResult(intent222, 0);
//                CommonFunc.startAction(context, cls, flag);
            }
        });

        loginAction.loginAction(username, password);
    }



    @SuppressLint("WrongConstant")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("Fuiou", "resultCode--返回值："+resultCode);
        Log.i("TAG", "resultCode--返回值："+resultCode);
        switch (resultCode) {
            case Activity.RESULT_CANCELED:
                String reason = "";
                String traceNo = "";
                String batchNo = "";
                String ordernumber = "";
                if (data != null) {
                    Bundle b = data.getExtras();
                    if (b != null) {
                        reason = (String) b.get("reason");
                        traceNo = (String)b.getString("traceNo");
                        batchNo = (String)b.getString("batchNo");
                        ordernumber = (String)b.getString("ordernumber");
                    }
                }
                if (reason != null) {
                    Log.d("reason", reason);
                    Toast.makeText(this, reason, Toast.LENGTH_SHORT).show();
                }
                Log.e("TAG", "失败返回值--reason--返回值："+reason+"/n 凭证号："+traceNo+"/n 批次号："+batchNo+"/n 订单号："+ordernumber);

                break;
            case Activity.RESULT_OK:
                String print = data.getStringExtra("reason");//打印成功返回数据
                Log.e("TAG", "成功返回值--reason--返回值："+print);

                String b = data.getStringExtra("batchNo");//批次号
                String c = data.getStringExtra("traceNo");//流水号
                String d = data.getStringExtra("cardNo");//卡号

                String e = data.getStringExtra("merchantId");//商户号
                String f = data.getStringExtra("terminalId");//终端号

                String g = data.getStringExtra("referenceNo");//参考号
                String h = data.getStringExtra("issue");//发卡行
                String i = data.getStringExtra("type");//卡类型
                String j = data.getStringExtra("date");//日期
                String k = data.getStringExtra("time");//时间

                String l = data.getStringExtra("wireless.apn");//apn
                String m = data.getStringExtra("wireless.username");//用户名
                String n = data.getStringExtra("wireless.password");//密码
                String o = data.getStringExtra("wireless.apnEnabled");//Apn是否开启
                String p = data.getStringExtra("merchantName");//商户名

                String r = data.getStringExtra("oldReferenceNo");//原参考号

                String t = data.getStringExtra("orderNumber");
                String tzfb = data.getStringExtra("zfbOrderNumber");
                String twx = data.getStringExtra("wxOrderNumber");

                String u = data.getStringExtra("oldOrderNumber");
                String sWx = data.getStringExtra("wxOldOrderNumber");
                String sZfb = data.getStringExtra("zfbOldOrderNumber");

                String wMb = data.getStringExtra("zfbMbOldOrderNumber");
                String zMb = data.getStringExtra("wxMbOldOrderNumber");

                String tui = data.getStringExtra("tuiOldOrderNumber");

                String settleData = data.getStringExtra("settleJson");
                String json = data.getStringExtra("json");

                String return_Code = data.getStringExtra("return_txt");//扫码返回数据

                String authorizationCode = data.getStringExtra("authorizationCode");//预授权 授权码

                String backOldReferenceNo = data.getStringExtra("backOldReferenceNo");//退货的原参考号

                String referenceNoSuccess = data.getStringExtra("referenceNoSuccess");//订单查询返回参考号

                String oldReferenceNoSuccess = data.getStringExtra("oldReferenceNoSuccess");//订单查询返回原参考号

                String amount = data.getStringExtra("amount");//金额

                String acqId = data.getStringExtra("acqId");//收单行

                String expiredDate = data.getStringExtra("expiredDate");//卡有效期

                JSONObject jsO = new JSONObject();
                try {
                    jsO.put("reason", print);
                    jsO.put("batchNo", b);//批次号
                    jsO.put("traceNo", c);//流水号
                    jsO.put("cardNo", d);//卡号
                    jsO.put("merchantId", e);//商户号
                    jsO.put("terminalId", f);//终端号
                    jsO.put("referenceNo", g);//参考号
                    jsO.put("issue", h);//发卡行
                    jsO.put("type", i);//卡类型
                    jsO.put("date", j);//日期
                    jsO.put("time", k);//时间
                    jsO.put("wireless.apn", l);//apn
                    jsO.put("wireless.username", m);//用户名
                    jsO.put("wireless.password", n);//密码
                    jsO.put("wireless.apnEnabled", o);//Apn是否开启
                    jsO.put("merchantName", p);//商户名
                    jsO.put("oldReferenceNo", r);//原参考号
                    jsO.put("backOldReferenceNo", backOldReferenceNo);//退货原参考号
                    jsO.put("orderNumber", t);//消费订单号
                    jsO.put("zfbOrderNumber", tzfb);//支付宝消费订单号
                    jsO.put("wxOrderNumber", twx);//微信消费订单号
                    jsO.put("oldOrderNumber", u);//原消费订单号
                    jsO.put("wxOldOrderNumber", sWx);//原支付宝消费订单号
                    jsO.put("zfbOldOrderNumber", sZfb);//原微信消费订单号
                    jsO.put("zfbMbOldOrderNumber",wMb);//原支付宝末笔订单号
                    jsO.put("wxMbOldOrderNumber", zMb);//原微信末笔订单号
                    jsO.put("tuiOldOrderNumber", tui);//退款订单号
                    jsO.put("return_txt", return_Code);//扫码返回数据
                    jsO.put("authorizationCode", authorizationCode);//授权码
                    jsO.put("referenceNoSuccess", referenceNoSuccess);//订单查询参考号
                    jsO.put("oldReferenceNoSuccess", oldReferenceNoSuccess);//订单查询原参考号
                    jsO.put("amount", amount);
                    jsO.put("acqId", acqId);
                    jsO.put("expiredDate", expiredDate);
                    jsO.put("settleJson", settleData);
                    jsO.put("json", json);

                } catch (JSONException e1) {
                    e1.printStackTrace();
                }

                ALog.json(jsO.toString());
                break;
        }
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_info_get:
                get();
                break;

            default:
                break;
        }
    }

    private void get() {
        if (NetUtils.isConnected(this)) {
            if (TextUtils.isEmpty(edUserName.getText().toString())) {
                ToastUtils.CustomShow(this, "请输入操作员号");
                return;
            }
            if (TextUtils.isEmpty(edPassWord.getText().toString())) {
                ToastUtils.CustomShow(this, "请输入操作员密码");
                return;
            }

            getInfo(edUserName.getText().toString(), edPassWord.getText().toString());


        } else {
            ToastUtils.CustomShow(this, "请打开网络");
        }

    }


}
